//
//  BaseNetWorkApi.swift
//  DemoNavBar
//
//  Created by YangHuan on 2019/12/12.
//  Copyright © 2019 YangHuan. All rights reserved.
//

import Foundation
import Moya

enum BaseNetWorkApi {
    case changeBadState(userId: String, id: String)
}


extension BaseNetWorkApi: TargetType {
   var method: Moya.Method {
       return .post
    }
    
    var sampleData: Data {
        return "{}".data(using: .utf8)!
    }
    
    var baseURL: URL {
        return URL(string: BaseUrl)!
    }
    
    var path: String {
        switch self {
        case .changeBadState(userId: _, id: _):
            return "njskada/"
        }
    }
    
    
    var task: Task {
        switch self {
        case .changeBadState(let ddd, let ssss):
            return .requestParameters(parameters: ["vvv": ddd, "dddd": ssss], encoding: URLEncoding.default)
       
        }
    }
    
    var headers: [String : String]? {
        switch self {
        default:
//            guard let token = UserDefaults.string(forKey: .token) else {return nil}
//            return ["token": token]
            return ["token" : ""]
        }
    }
    
    
}
extension BaseNetWorkApi {
    func dddd(ssss: [String : Any]) -> [String : Any] {
        var newDict: [String : Any]
        newDict = ssss
        for vvv in newDict.keys {
            if !CLLStringIsEmpty(value: newDict[vvv] as AnyObject) {
                newDict.removeValue(forKey: vvv)
            }
        }
        DLog(newDict)
      return newDict
    }
    
    
    
    
    func CLLStringIsEmpty(value: AnyObject?) -> Bool {
        //首先判断是否为nil
        if (nil == value) {
            //对象是nil，直接认为是空串
            return true
        }else{
            //然后是否可以转化为String
            if let myValue  = value as? String{
                //然后对String做判断
                return myValue == "" || myValue == "(null)" || 0 == myValue.count
            }else{
                //字符串都不是，直接认为是空串
                return true
            }
        }
    }
    
}
